package net.icontents.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.icontents.beans.User;
import net.icontents.dao.UserDao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl extends BaseORMDaoImpl<User> implements UserDao {

	public UserDaoImpl(){
		setNamespace("User");
	}
	
	public User getUserByEmail(String email){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("email", email);
		
		return queryForEntry("getUserByEmail", params);
	}
	
	public User getUserByUserName(String userName){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("userName", userName);
		
		return queryForEntry("getUserByUserName", params);
	}

	@Override
	public List<User> searchUser(String email) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("email", "%"+email+"%");
		
		return queryForEntryList("searchUser", params);
	}
	@Override
	public int searchUserCount(String email) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("email", "%"+email+"%");
		
		return queryForInteger("searchUserCount", params);
	}

	@Override
	public boolean isExist(String userName, String email) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("username", userName);
		params.put("email", email);
		
		return queryForInteger("isExist", params)>0;
	}

	@Override
	public boolean updateUserLevel(int id,int level) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		params.put("level", level);
		
		return update("updateUserLevel", params)==1;
	}
	@Override
	public boolean updateUserPassword(int id,String password) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		params.put("password", password);
		
		return update("updateUserPassword", params)==1;
	}
}